package dry.example.html;

import dry.example.utils.FileUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author zc
 */
public class Main {

    public static void main(String[] args) {
        String content = FileUtil.fileGetContents("D:\\api\\旺龙统一API文档.html");
        Document doc = Jsoup.parse(content);
        Elements tables = doc.getElementsByTag("table");
        StringBuffer sb = new StringBuffer();
        for (Element table : tables) {
            sb.append("<table>\n");
            Elements trs = table.select("tr");
            for (Element tr : trs) {
                sb.append("<tr>\n");
                Elements tds = tr.select("td");
                for (Element td : tds) {
                    String text = td.text();
                    StringBuffer temp = new StringBuffer();
                    if (td.hasAttr("rowspan")) {
                        temp.append(" rowspan=").append(td.attr("rowspan"));
                    }
                    if (td.hasAttr("colspan")) {
                        temp.append(" colspan=").append(td.attr("colspan"));
                    }
                    if (temp.length() > 0) {
                        sb.append("<td").append(temp).append(">");
                    } else {
                        sb.append("<td>");
                    }
                    sb.append(text);
                    sb.append("</td>\n");
                }
                sb.append("</tr>\n");
            }
            sb.append("</table>\n\n");
        }
        FileUtil.filePutContents("D:\\api\\table.html", sb.toString(), false);
    }

}
